package com.neusoft.ecommerce.controller;

import com.neusoft.ecommerce.dbhelper.LayuiPager;
import com.neusoft.ecommerce.entity.Goods;
import com.neusoft.ecommerce.entity.Indent;
import com.neusoft.ecommerce.entity.Trolley;
import com.neusoft.ecommerce.entity.UserMessage;
import com.neusoft.ecommerce.service.GoodsService;
import com.neusoft.ecommerce.service.IndentService;
import com.neusoft.ecommerce.service.TrolleyService;
import com.neusoft.ecommerce.service.UserMessageService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 87782
 */
@RestController
public class TrolleyController {
    private final TrolleyService trolleyService;
    private final UserMessageService userMessageService;
    private final GoodsService goodsService;

    public TrolleyController(TrolleyService trolleyService, UserMessageService userMessageService, GoodsService goodsService) {
        this.trolleyService = trolleyService;
        this.userMessageService = userMessageService;
        this.goodsService = goodsService;
    }

    @GetMapping("/trolley_page")
    public ModelAndView storagePage() {
        return new ModelAndView("trolley/trolley-list");
    }

    @RequestMapping("/trolley/list")
    @ResponseBody
    public LayuiPager<Trolley> list(@RequestParam(value = "page", defaultValue = "1") String page,
                                    @RequestParam(value = "limit", defaultValue = "10") String limit) {
        System.out.println(page + "" + limit);
        LayuiPager<Trolley> pager = new LayuiPager<>(Integer.parseInt(page), Integer.parseInt(limit));
        int count = trolleyService.findTotal(pager);
        List<Trolley> list = trolleyService.findList(pager);
        for (Trolley trolley : list) {
            System.out.println(trolley);
        }
        pager.setData(list);
        pager.setCount(count);
        pager.setCode(0);
        pager.setMsg("");
        return pager;
    }

    @GetMapping("/addTrolley")
    public ModelAndView addTrolley() {
        return new ModelAndView("trolley/trolley-add");
    }

    @RequestMapping("/trolley/userMessageList")
    public List<UserMessage> findUserMessageList() {
        return userMessageService.findAll();
    }

    @RequestMapping("/trolley/goodsList")
    public List<Goods> findGoodsList() {
        return goodsService.findAll();
    }

    @PostMapping("/trolley/add")
    public Map<String, Object> add(Trolley trolley) {
        System.out.println(trolley);
        Map<String, Object> map = new HashMap<>(16);
        if (trolleyService.add(trolley)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

    @GetMapping("/trolley/edit_page")
    public ModelAndView editPage(Trolley trolley, Model model) {
        System.out.println(trolley);
        LayuiPager<Trolley> pager = new LayuiPager<>(1,1);
        pager.setCondition(trolley);
        Trolley message = trolleyService.findList(pager).get(0);
        System.out.println(message);
        ModelAndView view = new ModelAndView("trolley/trolley-edit");
        model.addAttribute("trolley", message);
        return view;
    }

    @PostMapping("/trolley/edit")
    public Map<String, Object> edit(Trolley trolley) {
        System.out.println(trolley);
        Map<String, Object> map = new HashMap<>(16);
        if (trolleyService.edit(trolley)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

    @PostMapping("/trolley/delete")
    public Map<String, Object> delete(Trolley trolley) {
        System.out.println(trolley);
        Map<String, Object> map = new HashMap<>(16);
        if (trolleyService.delete(trolley)) {
            map.put("msg","ok");
            map.put("code",0);
        } else {
            map.put("msg","失败");
            map.put("code",1);
        }
        return map;
    }

}
